//
//  RecUserDetailsViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-20.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecUserDetailsViewController.h"

#import "RecUserDetailsModel.h"
#import "SDPhotoItem.h"
#import "SDPhotoGroup.h"
@interface RecUserDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation RecUserDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _sectionArr = [[NSMutableArray alloc] init];
    [self setUpPage];
    [self getData];
    _dataArr = [[NSMutableArray alloc] init];
    _srcStringArray = [[NSMutableArray alloc] init];
}
#pragma mark - 创建页面
-(void)setUpPage
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:UITableViewStyleGrouped];
    _table.separatorColor = [UIColor clearColor];
    _table.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    [self.view addSubview:_table];
    
    //头部
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250 * HEIGHT)];
    headerView.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    _table.tableHeaderView = headerView;
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180 * HEIGHT)];
    [image sd_setImageWithURL:[NSURL URLWithString:self.coverImage] placeholderImage:PLACEHOLDERIMAGE];
    [headerView addSubview:image];
    
    //名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH, 190 * HEIGHT, SCREEN_WIDTH - 100 * WIDTH, 60 * HEIGHT)];
    nameLabel.text = self.name;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont boldSystemFontOfSize:22.];
    [headerView addSubview:nameLabel];
}
#pragma mark - tableview Delegate & Datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * (_dataArr.count / 2) * HEIGHT;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    //相册
    SDPhotoGroup *photoGroup = [[SDPhotoGroup alloc] init];
    NSMutableArray *temp = [NSMutableArray array];
    [_srcStringArray enumerateObjectsUsingBlock:^(NSString *src, NSUInteger idx, BOOL *stop) {
        SDPhotoItem *item = [[SDPhotoItem alloc] init];
        item.thumbnail_pic = src;
        [temp addObject:item];
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    photoGroup.photoItemArray = [temp copy];
    [cell addSubview:photoGroup];
    cell.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    return cell;
}
#pragma mark - 数据获取
-(void)getData
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/?trip_id=%@",self.myID] block:^(id block) {
        for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"day_list"] ) {
            for (NSDictionary *dataDic in [dic objectForKey:@"spot_list"]) {
                NSString *str = [dataDic objectForKey:@"text"];
                [_sectionArr addObject:str];
                for (NSDictionary *lastDic in [dataDic objectForKey:@"detail_list"]) {
                    RecUserDetailsModel *model = [RecUserDetailsModel modelWithDictionary:lastDic];
                    [_dataArr addObject:model];
                    [_srcStringArray addObject:model.photo_w640];
                }
            }
        }
        _table.delegate = self;
        _table.dataSource = self;
        [_table reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
